package com.skyz.wrap.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.hjq.toast.ToastUtils;
import com.netease.nis.captcha.Captcha;
import com.netease.nis.captcha.CaptchaConfiguration;
import com.netease.nis.captcha.CaptchaListener;

public class CaptchaUitl {
    private final static  String id = "517c450ec40542be808e835fada765ce";
    public static interface Listener{
        void onSuc(String validate);
        void onError(int code ,String msg);
    }
    Listener listener;
    Captcha captcha;
    public void showCaptcha(Context context,Listener listener){
        if (context==null){
            return;
        }
        this.listener = listener;
        captcha = Captcha.getInstance().init(captchaConfiguration(context));
        captcha.validate();
    }

    private CaptchaConfiguration captchaConfiguration(Context context){
        CaptchaConfiguration captchaConfiguration = new CaptchaConfiguration.Builder().
                captchaId(id).
                //传统验证码类型
                mode(CaptchaConfiguration.ModeType.MODE_CAPTCHA).
                languageType(CaptchaConfiguration.LangType.LANG_ZH_CN).
                theme(CaptchaConfiguration.Theme.LIGHT).
                touchOutsideDisappear(false).
                listener(new CaptchaListener() {
                    @Override
                    public void onReady() {

                    }

                    @Override
                    public void onValidate(String result, String validate, String msg) {
                        Log.d("Validate","suc:"+result+":msg="+msg+":validate="+validate);
                        ToastUtils.show(msg);
                        if (listener!=null){
                            if (TextUtils.isEmpty(validate)){
                                listener.onError(0,msg);
                            }else {
                                listener.onSuc(validate);
                            }

                        }
                    }

                    @Override
                    public void onError(int code, String msg) {
                        ToastUtils.show(msg);
                        Log.d("Validate","code:"+code+":msg="+msg);
                        if (listener!=null){
                            listener.onError(code,msg);
                        }
                    }

                    @Override
                    public void onClose(Captcha.CloseType closeType) {
                        Log.d("Validate","CloseType:"+closeType);
                    }
                })
                .build(context);
        return captchaConfiguration;
    }


    public void destroy(){
        if (captcha!=null){
            captcha.destroy();
        }
    }

    public void dismissAllDialog(){
        if (captcha!=null){
            captcha.dismissAllDialog();
        }
    }
}
